package com.forum.message.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * RabbitMQ配置类
 */
@Configuration
public class RabbitMQConfig {
    
    /**
     * 交换机名称
     */
    public static final String MESSAGE_EXCHANGE = "message-exchange";
    
    /**
     * 队列名称
     */
    public static final String AUDIT_QUEUE = "message-queue-audit";
    public static final String LIKE_QUEUE = "message-queue-like";
    public static final String COMMENT_QUEUE = "message-queue-comment";
    public static final String MENTION_QUEUE = "message-queue-mention";
    
    /**
     * 路由键
     */
    public static final String AUDIT_ROUTING_KEY = "message.audit";
    public static final String LIKE_ROUTING_KEY = "message.like";
    public static final String COMMENT_ROUTING_KEY = "message.comment";
    public static final String MENTION_ROUTING_KEY = "message.mention";
    
    /**
     * 配置消息转换器
     */
    @Bean
    public MessageConverter messageConverter() {
        // 创建支持Java 8时间类型的ObjectMapper
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.registerModule(new JavaTimeModule());
        objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        
        // 创建Jackson2JsonMessageConverter并设置ObjectMapper
        return new Jackson2JsonMessageConverter(objectMapper);
    }
    
    /**
     * 配置RabbitTemplate
     */
    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory, MessageConverter messageConverter) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setMessageConverter(messageConverter);
        return rabbitTemplate;
    }
    
    /**
     * 配置消息交换机
     * Topic类型，根据routing key路由到不同队列
     */
    @Bean
    public TopicExchange messageExchange() {
        return new TopicExchange(MESSAGE_EXCHANGE);
    }
    
    /**
     * 配置审核消息队列
     */
    @Bean
    public Queue auditQueue() {
        return QueueBuilder.durable(AUDIT_QUEUE).build();
    }
    
    /**
     * 配置点赞消息队列
     */
    @Bean
    public Queue likeQueue() {
        return QueueBuilder.durable(LIKE_QUEUE).build();
    }
    
    /**
     * 配置评论消息队列
     */
    @Bean
    public Queue commentQueue() {
        return QueueBuilder.durable(COMMENT_QUEUE).build();
    }
    
    /**
     * 配置提及消息队列
     */
    @Bean
    public Queue mentionQueue() {
        return QueueBuilder.durable(MENTION_QUEUE).build();
    }
    
    /**
     * 绑定审核消息队列到交换机
     */
    @Bean
    public Binding auditBinding() {
        return BindingBuilder.bind(auditQueue()).to(messageExchange()).with(AUDIT_ROUTING_KEY);
    }
    
    /**
     * 绑定点赞消息队列到交换机
     */
    @Bean
    public Binding likeBinding() {
        return BindingBuilder.bind(likeQueue()).to(messageExchange()).with(LIKE_ROUTING_KEY);
    }
    
    /**
     * 绑定评论消息队列到交换机
     */
    @Bean
    public Binding commentBinding() {
        return BindingBuilder.bind(commentQueue()).to(messageExchange()).with(COMMENT_ROUTING_KEY);
    }
    
    /**
     * 绑定提及消息队列到交换机
     */
    @Bean
    public Binding mentionBinding() {
        return BindingBuilder.bind(mentionQueue()).to(messageExchange()).with(MENTION_ROUTING_KEY);
    }
} 